/** 关于时间的一些方法 */

/**
 * 获取本周、本月时间段
 * @param { number } type - 0: 本周(默认)  1: 本月
 */
export const getCurrentDate = (type = 0) => {
    let start = {}
    let end = {}
    const dayTime = 1000 * 60 * 60 * 24
    const date = new Date()
    const index = date.getDay()

    if (type === 0) {
        const startArray = [6, 0, 1, 2, 3, 4, 5]
        const endArray = [0, 6, 5, 4, 3, 2, 1]
        const currentTime = date.getTime()
        const startDate = new Date(currentTime - startArray[index] * dayTime)
        const endDate = new Date(currentTime + endArray[index] * dayTime)

        start = handleFormatDate(startDate)
        end = handleFormatDate(endDate)
    }
    else if (type === 1) {
        const year = date.getFullYear()
        const monthIndex = date.getMonth()
        const startDate = new Date(year, monthIndex)
        const endDate = new Date(year, monthIndex + 1)

        start = handleFormatDate(startDate)
        end = handleFormatDate(new Date(endDate.getTime() - dayTime))
    }
    return {start, end}
}

/**
 * 转换时间格式
 *  @param { Date|number|string }             date        时间
 *  @param { number }           index       选择的类型
 *  @param { Array<string>[2] } splitStrs   年月日的分割符号、时分秒的分割符号
 */
export const handleFormatDate = (_date, index = 1, splitStrs = ['-', ':']) => {
	const date = new Date(_date);
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    const hours = date.getHours().toString().padStart(2, '0')
    const minutes = date.getMinutes().toString().padStart(2, '0')
    const seconds = date.getSeconds().toString().padStart(2, '0')

    switch (index) {
        case 1:
            return year + splitStrs[0] + month + splitStrs[0] + day

        case 2:
            return year + splitStrs[0] + month + splitStrs[0] + day + ' ' + hours + splitStrs[1] + minutes

        case 3:
            return year + splitStrs[0] + month + splitStrs[0] + day + ' ' + hours + splitStrs[1] + minutes + splitStrs[1] + seconds

        case 4:
            return month + splitStrs[0] + day + ' ' + hours + splitStrs[1] + minutes

        case 5:
            return hours + splitStrs[1] + minutes

        default:
            return null
    }
}


/**
 * 获取当天的最后一刻 - 返回时间戳
 * @param {Date} date
 * */
export const getCurrentDateLastTime = (date) => {
    return (new Date(`${handleFormatDate(new Date(date))} 23:59:59`)).getTime();
}

/**
 * 判断第一个日期是否大于第二个日期
 * @param {Date|string|number} date1
 * @param {Date|string|number} date2
 * @return {boolean}
 */
export const compareDate = (date1, date2) => {
	console.log(date1,date2 ,'比较')
	return (new Date(date1)).getTime() > (new Date(date2)).getTime();
}
